/**********************************************************************
  The MIT License (MIT)
  Copyright (c) 2012 sunriseyuen

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ***********************************************************************/

#define _DEF_CCY_CODE
#define _ZZDEBUG
#define _PROJ_DC

#region Using directives
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

using Muki.Framework.Util;
using Muki.Framework.Business;
using Muki.Framework;
using Muki.UserDefine;
using Muki.Entity;
using Dot.StreamObject;

#endregion

namespace Muki.UserPanel
{
	public class UID02204BM:BLogic
	{
		public UID02204BM()
		{
			objDefault.UID_CODE    ="UID02204B";
		}

		public void Initialize()
		{
			objDefault.ZZLOGON_SSD = this.ZZLOGON_SSD;
			objDefault.Trans       = this.Trans;
			objDefault.ZZLOGON_ID  = this.ZZLOGON_ID;

			_Convert.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_Convert.Trans       = this.Trans;

			_DataPermission.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_DataPermission.Trans       = this.Trans;
			_DataPermission.TABLE_NAME  = "VORDTLM";
			_DataPermission.ZZLOGON_ID  = this.ZZLOGON_ID;
			_DataPermission.DataAccessRight();

			_DocsPermission.ZZLOGON_SSD  = this.ZZLOGON_SSD;
			_DocsPermission.Trans        = this.Trans;

			_Setting.ZZLOGON_ID = this.ZZLOGON_ID;
			_Setting.Trans      = this.Trans;

		}

		public UID02204BE Verify(UID02204BE _Entity)
		{
				_Entity.SIGN_CODE=_Entity.SIGN_CODE.ToUpper();
				//if (_Entity.AP_CCY_AMT==0)
				//{
	      //  			throw new LogisticException("AP_CCY_AMT","DataMiss","Validate");
				//}

						_Entity.VO_DT=_Entity.VO_DT.Trim();

					if (Util.IsNullOrEmpty(_Entity.VO_NO))
					{
						_Entity.Exceptions.Add(new LogisticException("VO_NO","DataMiss"));
					}

					if (Util.IsNullOrEmpty(_Entity.VO_DATE))
					{
						_Entity.Exceptions.Add(new LogisticException("VO_DATE","DataMiss"));
					}

					if (Util.IsNullOrEmpty(_Entity.ACC_CODE))
					{
						_Entity.Exceptions.Add(new LogisticException("ACC_CODE","DataMiss"));
					}

					if (Util.IsNullOrEmpty(_Entity.CCY_CODE))
					{
						_Entity.Exceptions.Add(new LogisticException("CCY_CODE","DataMiss"));
					}

					if (Util.IsNullOrEmpty(_Entity.XCHG_RATE))
					{
						_Entity.Exceptions.Add(new LogisticException("XCHG_RATE","DataMiss"));
					}

					string cUGET_XREF="{D,C}";
					if (cUGET_XREF.IndexOf(_Entity.SIGN_CODE)<0)
					{
						_Entity.Exceptions.Add(new LogisticException("SIGN_CODE","InvalidData",_Entity.SIGN_CODE));
					}

					if (Util.IsNullOrEmpty(_Entity.SIGN_CODE))
					{
						_Entity.Exceptions.Add(new LogisticException("SIGN_CODE","DataMiss"));
					}

					if (Util.IsNullOrEmpty(_Entity.VO_CCY_AMT))
					{
						_Entity.Exceptions.Add(new LogisticException("VO_CCY_AMT","DataMiss"));
					}

				// Relation ZUODTPTM
			if (!Util.IsNullOrEmpty(_Entity.ODTP_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ZUODTPTMEntity));//单据类型
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ODTP_CLASS_CODE","GLV");
			objCondition.Equals("ODTP_CODE",_Entity.ODTP_CODE);
				_DataPermission.TABLE_NAME="ZUODTPTM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

			if (objEntityContainer.Count==0)
			{
				_Entity.Exceptions.Add(new LogisticException("ODTP_CODE","DataNotFd",_Entity.ODTP_CODE));
			}else{

					ZUODTPTMEntity objVZUODTPTMEntity=(ZUODTPTMEntity)objEntityContainer[0];
					if (objVZUODTPTMEntity.Verified)
					{
						 _Entity.ODTP_CODE=objVZUODTPTMEntity.ODTP_CODE;

					}else{
						_Entity.Exceptions.Add(new LogisticException("ODTP_CODE","DataNotFd",_Entity.ODTP_CODE));
					}
			}
			}
//ZUODTPTM

				// Relation VORHDTM
			if (!Util.IsNullOrEmpty(_Entity.VO_NO))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(VORHDTMEntity));//GLRHDTM
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("VO_NO",_Entity.VO_NO);
				_DataPermission.TABLE_NAME="VORHDTM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

			if (objEntityContainer.Count==0)
			{
				_Entity.Exceptions.Add(new LogisticException("VO_NO","DataNotFd",_Entity.VO_NO));
			}else{

					VORHDTMEntity objVVORHDTMEntity=(VORHDTMEntity)objEntityContainer[0];
					if (objVVORHDTMEntity.Verified)
					{
						 _Entity.VO_NO=objVVORHDTMEntity.VO_NO;

					}else{
						_Entity.Exceptions.Add(new LogisticException("VO_NO","DataNotFd",_Entity.VO_NO));
					}
			}
			}
//VORHDTM

				// Relation ACCDTBM
			if (!Util.IsNullOrEmpty(_Entity.ACC_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ACCDTBMEntity));//Chart Of Account
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ACC_CODE",_Entity.ACC_CODE);
				_DataPermission.TABLE_NAME="ACCDTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

			if (objEntityContainer.Count==0)
			{
				_Entity.Exceptions.Add(new LogisticException("ACC_CODE","DataNotFd",_Entity.ACC_CODE));
			}else{

					ACCDTBMEntity objVACCDTBMEntity=(ACCDTBMEntity)objEntityContainer[0];
					if (objVACCDTBMEntity.Verified)
					{
						 _Entity.ACC_CODE=objVACCDTBMEntity.ACC_CODE;

					}else{
						_Entity.Exceptions.Add(new LogisticException("ACC_CODE","DataNotFd",_Entity.ACC_CODE));
					}
			}
			}
//ACCDTBM

				// Relation CCYRTBM
			if (!Util.IsNullOrEmpty(_Entity.CCY_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(CCYRTBMEntity));//货币资料表
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("CCY_CODE",_Entity.CCY_CODE);
				_DataPermission.TABLE_NAME="CCYRTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

			if (objEntityContainer.Count==0)
			{
				_Entity.Exceptions.Add(new LogisticException("CCY_CODE","DataNotFd",_Entity.CCY_CODE));
			}else{

					CCYRTBMEntity objVCCYRTBMEntity=(CCYRTBMEntity)objEntityContainer[0];
					if (objVCCYRTBMEntity.Verified)
					{
						 _Entity.CCY_CODE=objVCCYRTBMEntity.CCY_CODE;

					}else{
						_Entity.Exceptions.Add(new LogisticException("CCY_CODE","DataNotFd",_Entity.CCY_CODE));
					}
			}
			}
//CCYRTBM

				// Relation UOMTBLM
			if (!Util.IsNullOrEmpty(_Entity.UOM_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(UOMTBLMEntity));//单位资料
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("UOM_CODE",_Entity.UOM_CODE);
				_DataPermission.TABLE_NAME="UOMTBLM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

			if (objEntityContainer.Count==0)
			{
				_Entity.Exceptions.Add(new LogisticException("UOM_CODE","DataNotFd",_Entity.UOM_CODE));
			}else{

					UOMTBLMEntity objVUOMTBLMEntity=(UOMTBLMEntity)objEntityContainer[0];
					if (objVUOMTBLMEntity.Verified)
					{
						 _Entity.UOM_CODE=objVUOMTBLMEntity.UOM_CODE;

					}else{
						_Entity.Exceptions.Add(new LogisticException("UOM_CODE","DataNotFd",_Entity.UOM_CODE));
					}
			}
			}
//UOMTBLM

				// Relation PMTCDTM
			if (!Util.IsNullOrEmpty(_Entity.PAY_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(PMTCDTMEntity));//付款方式
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("PMT_CODE",_Entity.PAY_CODE);
				_DataPermission.TABLE_NAME="PMTCDTM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

			if (objEntityContainer.Count==0)
			{
				_Entity.Exceptions.Add(new LogisticException("PAY_CODE","DataNotFd",_Entity.PAY_CODE));
			}else{

					PMTCDTMEntity objVPMTCDTMEntity=(PMTCDTMEntity)objEntityContainer[0];
					if (objVPMTCDTMEntity.Verified)
					{
						 _Entity.PAY_CODE=objVPMTCDTMEntity.PMT_CODE;

					}else{
						_Entity.Exceptions.Add(new LogisticException("PAY_CODE","DataNotFd",_Entity.PAY_CODE));
					}
			}
			}
//PMTCDTM

						CriteriaRetrieve objValidateEntity=new CriteriaRetrieve(typeof(VORHDTMEntity));
						objCondition=objValidateEntity.NewCondition();
						objCondition.Equals("VO_NO",_Entity.VO_NO);
			
						EntityObject objValidateObject=this.Trans.RetrieveEntity(objValidateEntity);

						if (objValidateObject!=null)
						{
							VORHDTMEntity objVORHDTMEntity=(VORHDTMEntity)objValidateObject;
							
							ZZStatusCheck oZZStatusCheck=new ZZStatusCheck();
							oZZStatusCheck.Trans=this.Trans;
							if (!oZZStatusCheck.Validate("ODTP_CODE_DT",objVORHDTMEntity.ODTP_STATUS,_Entity.ATVT_CODE))
							{
  							throw new LogisticException("ODTP_STATUS",oZZStatusCheck.Caption,objVORHDTMEntity.ODTP_STATUS);
							}
						}

				return _Entity;
		}

		public List<UID02204BE> Verify(List<UID02204BE> _Entitys)
		{
			List<UID02204BE> _New_Entitys=new List<UID02204BE>();

			foreach (UID02204BE _Entity in _Entitys) 
			{
				_New_Entitys.Add(Verify(_Entity));
			}
			return _New_Entitys;
		}

		public UID02204BE Default(UID02204BE _Entity)
		{

						_Entity.ODTP_CODE=objDefault.getDefault("ODTP_CODE",_Entity.ODTP_CODE);
						_Entity.VO_DT=objDefault.getDefault("VO_DT",_Entity.VO_DT);
						_Entity.ODTP_STATUS=objDefault.getDefault("ODTP_STATUS",_Entity.ODTP_STATUS);
						_Entity.VO_DATE=objDefault.getDefault("VO_DATE",_Entity.VO_DATE);
						_Entity.COMP_CODE=objDefault.getDefault("COMP_CODE",_Entity.COMP_CODE);
						_Entity.REVERSE_BIT=objDefault.getDefault("REVERSE_BIT",_Entity.REVERSE_BIT);
						_Entity.REFX_NO=objDefault.getDefault("REFX_NO",_Entity.REFX_NO);
						_Entity.ACC_CODE=objDefault.getDefault("ACC_CODE",_Entity.ACC_CODE);
						_Entity.ACC_NAME01=objDefault.getDefault("ACC_NAME01",_Entity.ACC_NAME01);
						_Entity.CCY_CODE=objDefault.getDefault("CCY_CODE",_Entity.CCY_CODE);
						_Entity.XCHG_RATE=objDefault.getDefault("XCHG_RATE",_Entity.XCHG_RATE);
						_Entity.SERIAL_NO=objDefault.getDefault("SERIAL_NO",_Entity.SERIAL_NO);
						_Entity.SIGN_CODE=objDefault.getDefault("SIGN_CODE",_Entity.SIGN_CODE);
						_Entity.VO_CCY_AMT=objDefault.getDefault("VO_CCY_AMT",_Entity.VO_CCY_AMT);
						_Entity.VO_DET_AMT=objDefault.getDefault("VO_DET_AMT",_Entity.VO_DET_AMT);
						_Entity.VO_DOC_NAME01=objDefault.getDefault("VO_DOC_NAME01",_Entity.VO_DOC_NAME01);
						_Entity.VO_RMKS=objDefault.getDefault("VO_RMKS",_Entity.VO_RMKS);
						_Entity.REQ_QTY=objDefault.getDefault("REQ_QTY",_Entity.REQ_QTY);
						_Entity.UOM_CODE=objDefault.getDefault("UOM_CODE",_Entity.UOM_CODE);
						_Entity.DOC_NO=objDefault.getDefault("DOC_NO",_Entity.DOC_NO);
						_Entity.DOC_TP_CODE=objDefault.getDefault("DOC_TP_CODE",_Entity.DOC_TP_CODE);
						_Entity.PAY_CODE=objDefault.getDefault("PAY_CODE",_Entity.PAY_CODE);
						_Entity.DOC_REF_NO=objDefault.getDefault("DOC_REF_NO",_Entity.DOC_REF_NO);
						_Entity.DOC_REQ_DATE=objDefault.getDefault("DOC_REQ_DATE",_Entity.DOC_REQ_DATE);
						_Entity.DOC_DUE_DATE=objDefault.getDefault("DOC_DUE_DATE",_Entity.DOC_DUE_DATE);
						_Entity.VO_DR_DET_AMT=objDefault.getDefault("VO_DR_DET_AMT",_Entity.VO_DR_DET_AMT);
						_Entity.VO_DR_CCY_AMT=objDefault.getDefault("VO_DR_CCY_AMT",_Entity.VO_DR_CCY_AMT);
						_Entity.VO_CR_DET_AMT=objDefault.getDefault("VO_CR_DET_AMT",_Entity.VO_CR_DET_AMT);
						_Entity.VO_CR_CCY_AMT=objDefault.getDefault("VO_CR_CCY_AMT",_Entity.VO_CR_CCY_AMT);
						_Entity.DR_DET_AMT=objDefault.getDefault("DR_DET_AMT",_Entity.DR_DET_AMT);
						_Entity.CR_DET_AMT=objDefault.getDefault("CR_DET_AMT",_Entity.CR_DET_AMT);
						_Entity.DIFF_DET_AMT=objDefault.getDefault("DIFF_DET_AMT",_Entity.DIFF_DET_AMT);

					// WriteDefault VORHDTM
ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_NO="+_Entity.VO_NO);
			if (!Util.IsNullOrEmpty(_Entity.VO_NO))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(VORHDTMEntity));//GLRHDTM
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("VO_NO",_Entity.VO_NO);
				_DataPermission.TABLE_NAME="VORHDTM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"VORHDTM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					VORHDTMEntity objDVORHDTMEntity=(VORHDTMEntity)objEntityContainer[0];
					if (objDVORHDTMEntity.Verified)
					{
						 _Entity.VO_NO=objDVORHDTMEntity.VO_NO;
						 _Entity.CCY_CODE=objDVORHDTMEntity.CCY_CODE;
						 _Entity.COMP_CODE=objDVORHDTMEntity.COMP_CODE;
						 _Entity.ODTP_CODE=objDVORHDTMEntity.ODTP_CODE;
						 _Entity.ODTP_STATUS=objDVORHDTMEntity.ODTP_STATUS;
						 _Entity.VO_DATE=objDVORHDTMEntity.VO_DATE;
						 _Entity.XCHG_RATE=objDVORHDTMEntity.XCHG_RATE;

					}else{
					}
			}else{
			}
			}

					// WriteDefault ACCDTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.ACC_CODE="+_Entity.ACC_CODE);
			if (!Util.IsNullOrEmpty(_Entity.ACC_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ACCDTBMEntity));//Chart Of Account
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ACC_CODE",_Entity.ACC_CODE);
				_DataPermission.TABLE_NAME="ACCDTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"ACCDTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					ACCDTBMEntity objDACCDTBMEntity=(ACCDTBMEntity)objEntityContainer[0];
					if (objDACCDTBMEntity.Verified)
					{
						 _Entity.ACC_CODE=objDACCDTBMEntity.ACC_CODE;
						 _Entity.ACC_NAME01=objDACCDTBMEntity.ACC_NAME01;
						 _Entity.SIGN_CODE=objDACCDTBMEntity.SIGN_CODE;

					}else{
					}
			}else{
			}
			}

					// WriteDefault CCYRTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.CCY_CODE="+_Entity.CCY_CODE);
			if (!Util.IsNullOrEmpty(_Entity.CCY_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(CCYRTBMEntity));//货币资料表
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("CCY_CODE",_Entity.CCY_CODE);
				_DataPermission.TABLE_NAME="CCYRTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"CCYRTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					CCYRTBMEntity objDCCYRTBMEntity=(CCYRTBMEntity)objEntityContainer[0];
					if (objDCCYRTBMEntity.Verified)
					{
						 _Entity.CCY_CODE=objDCCYRTBMEntity.CCY_CODE;
						 _Entity.XCHG_RATE=objDCCYRTBMEntity.XCHG_RATE;

					}else{
					}
			}else{
			}
			}

				decimal cTOTAL_VO_DR_AMT=0;
				decimal cTOTAL_AP_CR_AMT=0;
				
				QueryRows RsVORDTLM = new QueryRows(this.Trans);
				RsVORDTLM.CommandText = "SELECT * FROM VORDTLM WHERE VO_NO='"+_Entity.VO_NO+"'";
				RsVORDTLM.Open();
				while (!RsVORDTLM.EOF)
				{
					if (RsVORDTLM.getValue("SIGN_CODE")=="D")
					{
						cTOTAL_VO_DR_AMT=cTOTAL_VO_DR_AMT+Math.Abs(RsVORDTLM.getDecimal("VO_DR_DET_AMT"));
					}else{
						cTOTAL_AP_CR_AMT=cTOTAL_AP_CR_AMT+Math.Abs(RsVORDTLM.getDecimal("VO_CR_DET_AMT"));
					}
					RsVORDTLM.MoveNext();
				}
      	RsVORDTLM.Close();
  
				_Entity.DR_DET_AMT=cTOTAL_VO_DR_AMT;
				_Entity.CR_DET_AMT=cTOTAL_AP_CR_AMT;
				_Entity.DIFF_DET_AMT=_Entity.DR_DET_AMT-_Entity.CR_DET_AMT;

				return _Entity;
		}

		public List<UID02204BE> Default(List<UID02204BE> _Entitys)
		{
			List<UID02204BE> _New_Entitys=new List<UID02204BE>();
			foreach (UID02204BE _Entity in _Entitys) 
			{
				_New_Entitys.Add(Default(_Entity));
			}
			return _New_Entitys;
		}

		public UID02204BE Search(VContexts _VContexts,UID02204BE _Entity)
		{

			if (_DocsPermission.AccessRight("UID02204B", this.ZZLOGON_ID, "ACCESS")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02204B","NotAccessRight", this.ZZLOGON_ID));
				return _Entity;
			}

			ZZSQLStatement  _SQLStatement = new ZZSQLStatement();

			_SQLStatement.Trans         = this.Trans;
			_SQLStatement.UID_CODE      = "UID02204B";
			_SQLStatement.ZZLOGON_SSD   = this.ZZLOGON_SSD;
			_SQLStatement.ZZLOGON_LANG  = this.ZZLOGON_LANG;
			_SQLStatement.DbName        = this.DbName;
			_SQLStatement.Process("QID02204B");

			ZZQLite oZZQLite = _SQLStatement.oZZQLite;

			if (_VContexts.getValue("T1")!="" && _VContexts.getValue("V1")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T1"), _VContexts.getValue("V1"));
			}
			if (_VContexts.getValue("T2")!="" && _VContexts.getValue("V2")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T2"), _VContexts.getValue("V2"));
			}
			if (_VContexts.getValue("T3")!="" && _VContexts.getValue("V3")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T3"), _VContexts.getValue("V3"));
			}

			if (!Util.IsNullOrEmpty(_VContexts.getValue("lookup")))
			{
				oZZQLite.AddXWhereClause("A", "", _VContexts.getValue("lookup"));
			}

						_VContexts.setValue("TT2","VORDTLM.VO_NO");
						_VContexts.setValue("VV2",_Entity.VO_NO);
						oZZQLite.AddWhereClause("VORDTLM", "VO_NO", _Entity.VO_NO);

			oZZQLite.Condition=_VContexts.getValue("Condition");
			oZZQLite.Make();

			int _AbsolutePage = _VContexts.getInteger("AbsolutePage");
			int _PageSize     = _VContexts.getInteger("ZZPageSize");
			int _Restrict     = _VContexts.getInteger("Restrict");
			int _TotalPages   = 0;

			_Setting.Trans      = this.Trans;

			if (_Restrict == 0)
			{
				_Restrict=30;
			}

			if (_PageSize <= 0)
			{
				_PageSize=_Setting.getInteger("ZZPageSize","20");
			}

				QueryRows RsVORDTLM   = new QueryRows(this.Trans);
			RsVORDTLM.CommandText = oZZQLite.SQLStatement;
			RsVORDTLM.Top         = _Restrict;
			RsVORDTLM.Skip        = (_AbsolutePage-1)*_PageSize;
			RsVORDTLM.Limit       = _PageSize;
			RsVORDTLM.CountAll    = true;
			RsVORDTLM.Open();

			if (_PageSize <= 0)
			{
				_TotalPages   = RsVORDTLM.RecordCount;
				_PageSize = RsVORDTLM.RecordCount;
			}
			else
			{
				_TotalPages= RsVORDTLM.RecordCount / _PageSize;
			}
			if (RsVORDTLM.RecordCount > (RsVORDTLM.RecordCount / _PageSize) * _PageSize)
			{
				_TotalPages = _TotalPages+ 1;
			}

			if (_AbsolutePage <= 0)
			{
				_AbsolutePage = 1;
			}

			if (_AbsolutePage > _TotalPages)
			{
				_AbsolutePage = _TotalPages;
			}

			Wxf _Wxf  = new Wxf();

			_Wxf.XDeclareStart();
			foreach(AttributeMapping _AttributeMapping in oZZQLite.AttributeMappings)
			{
				if (_AttributeMapping.STATUS_CODE != "HIDDEN")
				{
					_Wxf.XDeclare(_AttributeMapping);
				}
			}
			_Wxf.XDeclareFinal();

			_Wxf.Variable("ZZPageSize",_PageSize);
			_Wxf.Variable("ZZPages",_TotalPages);
			_Wxf.Variable("USQL_CODE","QID02204B");

			while (!RsVORDTLM.EOF)
			{
				string cPickUpData   = "";
				object cValue;

				_Wxf.AddNew();
				int i=0;
				foreach(AttributeMapping _AttributeMapping in oZZQLite.AttributeMappings)
				{

					cValue = RsVORDTLM[i];

					if (_AttributeMapping.STATUS_CODE != "HIDDEN")
					{
						_Wxf[_AttributeMapping.Name] = cValue;
					}

					if (_AttributeMapping.PrimaryKey)
					{
						if (cPickUpData != "")
						{
							cPickUpData = cPickUpData + "|";
						}
						cPickUpData = cPickUpData + ZZGeneral.Base64Encoder(_AttributeMapping.ColumnName + "=" + cValue);
					}
					i++;
				}

				_Wxf.PK_DATA   = cPickUpData;
				_Wxf.Update();

				RsVORDTLM.MoveNext();
			}
			RsVORDTLM.Close();

			_Entity.Wxf=_Wxf;

			return _Entity;
		}

		public UID02204BE Retrieves(VContexts _VContexts,UID02204BE _Entity)
		{
			if (_DocsPermission.AccessRight("UID02204B", this.ZZLOGON_ID, "ACCESS")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02204B","NotAccessRight",this.ZZLOGON_ID));
				return _Entity;
			}

			List<UID02204BE> _New_Entitys=new List<UID02204BE>();

				CriteriaRetrieve objRetrieveVORDTLMEntity=new CriteriaRetrieve(typeof(VORDTLMEntity));

			objRetrieveVORDTLMEntity.Top=1;
			objCondition=objRetrieveVORDTLMEntity.NewCondition();

			bool ZZOrderByAsc=_Setting.getBoolean("ZZOrderByAsc","YES");
			switch (_VContexts.getValue("UDEF_CODE"))
			{
				case "MF":
					{
						objRetrieveVORDTLMEntity.OrderBy("VO_DT",ZZOrderByAsc);
						break;
					}
				case "ML":
					{
						objRetrieveVORDTLMEntity.OrderBy("VO_DT",!ZZOrderByAsc);
						break;
					}
				case "MN":
					{
						if (ZZOrderByAsc)
						{
							objCondition.GreaterThan("VO_DT",_Entity.VO_DT);
						}else{
							objCondition.LessThan("VO_DT",_Entity.VO_DT);
						}
						objRetrieveVORDTLMEntity.OrderBy("VO_DT",ZZOrderByAsc);
						break;
					}
				case "MP":
					{
						if (ZZOrderByAsc)
						{
							objCondition.LessThan("VO_DT",_Entity.VO_DT);
						}else{
							objCondition.GreaterThan("VO_DT",_Entity.VO_DT);
						}
						objRetrieveVORDTLMEntity.OrderBy("VO_DT",!ZZOrderByAsc);
						break;
					}
				case "CANCEL":
					{
						objRetrieveVORDTLMEntity.OrderBy("VO_DT",ZZOrderByAsc);
						_Entity.DATA_STATUS_CODE="Verified";
						break;
					}
				case "LOOKUP":
					{
						objRetrieveVORDTLMEntity.OrderBy("VO_DT",ZZOrderByAsc);
						break;
					}

				case "ACTIVETAB":
					{
								if (_Entity.VO_DT != "")
								{
									objCondition.Equals("VO_DT", _Entity.VO_DT);
								}

						objRetrieveVORDTLMEntity.OrderBy("VO_DT",ZZOrderByAsc);
						break;
					}

				case "READ":
					{
								objCondition.Equals("VO_DT", _Entity.VO_DT);
						break;
					}					
				default:
					{
								if (_Entity.VO_DT != "")
								{
									objCondition.Equals("VO_DT", _Entity.VO_DT);
								}
						objRetrieveVORDTLMEntity.OrderBy("VO_DT",ZZOrderByAsc);

						break;
					}
			}

			//filter by quick look
			string _LOOUP_COLUMN_NAME=";VORDTLM.VO_NO;VORDTLM.VO_DT;VORDTLM.ACC_CODE;VORDTLM.CCY_CODE;VORDTLM.SIGN_CODE;VORDTLM.VO_DOC_NAME01;VORDTLM.VO_RMKS;VORDTLM.UOM_CODE;VORDTLM.DEPT_CODE;VORDTLM.VO_ANAY_CODE01;VORDTLM.VO_ANAY_CODE02;VORDTLM.VO_ANAY_CODE03;VORDTLM.VO_ANAY_CODE04;VORDTLM.VO_ANAY_CODE05;VORDTLM.VO_ANAY_CODE06;VORDTLM.DOC_NO;VORDTLM.DOC_TP_CODE;VORDTLM.PAY_CODE;VORDTLM.DOC_REF_NO";
			string lookup=_VContexts.getValue("lookup");
			_LOOUP_COLUMN_NAME=_LOOUP_COLUMN_NAME+";";
			if (lookup!="")
			{
				Condition _Condition2=objCondition.NewCondition(" AND ");
				foreach (string s in _LOOUP_COLUMN_NAME.Split(new char[] { ';' }))
				{
					if (!string.IsNullOrEmpty(s))
					{
						Condition OrCondition=_Condition2.NewCondition();
						OrCondition.Expression(s,lookup);
					}
				}
			}

			//filter by filters

				//filter by AccessConditions
				_DataPermission.AccessCondition(objCondition);

						objCondition.Equals("VO_NO", _Entity.VO_NO);

			VORDTLMEntity objVORDTLMs=(VORDTLMEntity)this.Trans.RetrieveEntity(objRetrieveVORDTLMEntity);

			ZZTrace.Debug(ZFILE_NAME,"SELECT="+objRetrieveVORDTLMEntity.SELECTClause);

			if (objVORDTLMs==null && _VContexts.getValue("UDEF_CODE")=="MN")
			{
				_Entity.Exceptions.Add(new LogisticException("VO_DT","DataEOF"));
				return _Entity;
			}else if (objVORDTLMs==null && _VContexts.getValue("UDEF_CODE")=="MP")
			{
				_Entity.Exceptions.Add(new LogisticException("VO_DT","DataBOF"));
				return _Entity;
			}else if (objVORDTLMs==null && (_VContexts.getValue("UDEF_CODE")=="MF" || _VContexts.getValue("UDEF_CODE")=="ML") )
			{
				_Entity.Exceptions.Add(new LogisticException("VO_DT","DataBlank"));
				return _Entity;
			}else if (objVORDTLMs==null && _VContexts.getValue("UDEF_CODE")=="LOOKUP")
			{
				/*
				 *if (this.COLUMN_REFX.IndexOf(";")>=0)
				 *{
				 *}else{
				 *    string _T_COLUMN_REFX=this.COLUMN_REFX;
				 *    if (_T_COLUMN_REFX.IndexOf('.') > 0)
				 *    {
				 *        string[] s = _T_COLUMN_REFX.Split(new char[] { '.' });
				 *        _T_COLUMN_REFX=s[1];
				 *    }
				 *    _Entity.Exceptions.Add(new LogisticException(_T_COLUMN_REFX,"DataNotFd","DataBlank"));
				 *    return _Entity;
				 *}
				 */
			}else if (objVORDTLMs==null && _VContexts.getValue("UDEF_CODE")=="ACTIVETAB")
			{
				//throw new LogisticException("VO_DT","Activetab","");
				//this.Default(_Entity);
			}else if (objVORDTLMs==null)
			{
				//this.Default(_Entity);
			}else{
				VORDTLMEntity objVORDTLMEntity = objVORDTLMs;

			if (objVORDTLMEntity.SIGN_CODE=="D")
			{
				_Entity.VO_DET_AMT=objVORDTLMEntity.VO_DR_DET_AMT;
				_Entity.VO_CCY_AMT=objVORDTLMEntity.VO_DR_CCY_AMT;
			}else{
				_Entity.VO_DET_AMT=objVORDTLMEntity.VO_CR_DET_AMT;
				_Entity.VO_CCY_AMT=objVORDTLMEntity.VO_CR_CCY_AMT;
			}

					ZZTrace.MUKI_DEBUG_LOG=true;
						_Entity.VO_NO=objVORDTLMEntity.VO_NO;
						_Entity.VO_DT=objVORDTLMEntity.VO_DT;
						_Entity.ACC_CODE=objVORDTLMEntity.ACC_CODE;
						_Entity.CCY_CODE=objVORDTLMEntity.CCY_CODE;
						_Entity.XCHG_RATE=objVORDTLMEntity.XCHG_RATE;
						_Entity.SERIAL_NO=objVORDTLMEntity.SERIAL_NO;
						_Entity.SIGN_CODE=objVORDTLMEntity.SIGN_CODE;
						_Entity.VO_DOC_NAME01=objVORDTLMEntity.VO_DOC_NAME01;
						_Entity.VO_RMKS=objVORDTLMEntity.VO_RMKS;
						_Entity.REQ_QTY=objVORDTLMEntity.REQ_QTY;
						_Entity.UOM_CODE=objVORDTLMEntity.UOM_CODE;
						_Entity.DOC_NO=objVORDTLMEntity.DOC_NO;
						_Entity.DOC_TP_CODE=objVORDTLMEntity.DOC_TP_CODE;
						_Entity.PAY_CODE=objVORDTLMEntity.PAY_CODE;
						_Entity.DOC_REF_NO=objVORDTLMEntity.DOC_REF_NO;
						_Entity.DOC_REQ_DATE=objVORDTLMEntity.DOC_REQ_DATE;
						_Entity.DOC_DUE_DATE=objVORDTLMEntity.DOC_DUE_DATE;
						_Entity.VO_DR_DET_AMT=objVORDTLMEntity.VO_DR_DET_AMT;
						_Entity.VO_DR_CCY_AMT=objVORDTLMEntity.VO_DR_CCY_AMT;
						_Entity.VO_CR_DET_AMT=objVORDTLMEntity.VO_CR_DET_AMT;
						_Entity.VO_CR_CCY_AMT=objVORDTLMEntity.VO_CR_CCY_AMT;

				_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objVORDTLMEntity.ZADD_DATE);
_Entity.UZADD_USER     =objVORDTLMEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objVORDTLMEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objVORDTLMEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objVORDTLMEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objVORDTLMEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objVORDTLMEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objVORDTLMEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objVORDTLMEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objVORDTLMEntity.ZSECURITY_CODE6;

					// WriteDefault VORHDTM
ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_NO="+_Entity.VO_NO);
			if (!Util.IsNullOrEmpty(_Entity.VO_NO))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(VORHDTMEntity));//GLRHDTM
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("VO_NO",_Entity.VO_NO);
				_DataPermission.TABLE_NAME="VORHDTM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"VORHDTM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					VORHDTMEntity objDVORHDTMEntity=(VORHDTMEntity)objEntityContainer[0];
					if (objDVORHDTMEntity.Verified)
					{
						 _Entity.VO_NO=objDVORHDTMEntity.VO_NO;
						 _Entity.COMP_CODE=objDVORHDTMEntity.COMP_CODE;
						 _Entity.ODTP_CODE=objDVORHDTMEntity.ODTP_CODE;
						 _Entity.ODTP_STATUS=objDVORHDTMEntity.ODTP_STATUS;
						 _Entity.VO_DATE=objDVORHDTMEntity.VO_DATE;

					}else{
					}
			}else{
			}
			}

					// WriteDefault ACCDTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.ACC_CODE="+_Entity.ACC_CODE);
			if (!Util.IsNullOrEmpty(_Entity.ACC_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ACCDTBMEntity));//Chart Of Account
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ACC_CODE",_Entity.ACC_CODE);
				_DataPermission.TABLE_NAME="ACCDTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"ACCDTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					ACCDTBMEntity objDACCDTBMEntity=(ACCDTBMEntity)objEntityContainer[0];
					if (objDACCDTBMEntity.Verified)
					{
						 _Entity.ACC_CODE=objDACCDTBMEntity.ACC_CODE;
						 _Entity.ACC_NAME01=objDACCDTBMEntity.ACC_NAME01;

					}else{
					}
			}else{
			}
			}

				decimal cTOTAL_VO_DR_AMT=0;
				decimal cTOTAL_AP_CR_AMT=0;
				
				QueryRows RsVORDTLM = new QueryRows(this.Trans);
				RsVORDTLM.CommandText = "SELECT * FROM VORDTLM WHERE VO_NO='"+_Entity.VO_NO+"'";
				RsVORDTLM.Open();
				while (!RsVORDTLM.EOF)
				{
					if (RsVORDTLM.getValue("SIGN_CODE")=="D")
					{
						cTOTAL_VO_DR_AMT=cTOTAL_VO_DR_AMT+Math.Abs(RsVORDTLM.getDecimal("VO_DR_DET_AMT"));
					}else{
						cTOTAL_AP_CR_AMT=cTOTAL_AP_CR_AMT+Math.Abs(RsVORDTLM.getDecimal("VO_CR_DET_AMT"));
					}
					RsVORDTLM.MoveNext();
				}
      	RsVORDTLM.Close();
  
				_Entity.DR_DET_AMT=cTOTAL_VO_DR_AMT;
				_Entity.CR_DET_AMT=cTOTAL_AP_CR_AMT;
				_Entity.DIFF_DET_AMT=_Entity.DR_DET_AMT-_Entity.CR_DET_AMT;

					_Entity.DATA_STATUS_CODE="Verified";
			}
				return _Entity;
		}

		public  UID02204BE AddNew(UID02204BE _Entity)
		{

			ZZTrace.Debug(ZFILE_NAME,"ZZLOGON_ID="+this.ZZLOGON_ID);

			if (_DocsPermission.AccessRight("UID02204B", this.ZZLOGON_ID, "ADDNEW")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02204B","NotAddNewRight",this.ZZLOGON_ID));
				return _Entity;
			}

			_Entity.ATVT_CODE="ADDNEW";

				_Entity=this.Verify(_Entity);
			if (_Entity.Exceptions.Count>0)
			{
				return _Entity;
			}
			VORDTLMEntity objAddNewEntity=new VORDTLMEntity();

			ZZHDKeyLib objZZHDKeyLib=new ZZHDKeyLib();
			objZZHDKeyLib.Trans=this.Trans;
							objZZHDKeyLib.LNK_COLUMN("VO_NO",_Entity.VO_NO);
							objZZHDKeyLib.LNK_COLUMN_VALUE=_Entity.VO_NO;

				objZZHDKeyLib.PREFIX_CODE="UID02204B";
				objZZHDKeyLib.getSerial("VORDTLM");
				int XSERIAL_NO=objZZHDKeyLib.PREFIX_CN;

				_Entity.VO_DT=objZZHDKeyLib.DT_NO;

				ZZTrace.Debug(ZFILE_NAME,_Entity.VO_DT);
				ZZTrace.Debug(ZFILE_NAME,XSERIAL_NO);

					objAddNewEntity.VO_DT=_Entity.VO_DT;

			this.Trans.RetrieveEntity(objAddNewEntity);

			if (objAddNewEntity.Verified)
			{
				_Entity.Exceptions.Add(new LogisticException("VO_DT","DataExist",_Entity.VO_DT));
				return _Entity;
			}else{

				objAddNewEntity.ZADD_DATE = DateTime.Now;
				objAddNewEntity.ZADD_USER = this.ZZLOGON_ID;

				_DataPermission.TABLE_NAME="VORDTLM";
				_DataPermission.DataAccessRight();
				objAddNewEntity.ZSECURITY_CODE1 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE1");
				objAddNewEntity.ZSECURITY_CODE2 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE2");
				objAddNewEntity.ZSECURITY_CODE3 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE3");
				objAddNewEntity.ZSECURITY_CODE4 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE4");
				objAddNewEntity.ZSECURITY_CODE5 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE5");
				objAddNewEntity.ZSECURITY_CODE6 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE6");
				objAddNewEntity.ZSECURITY_RIGHT = _DataPermission.ACCESS_AUTH.getInteger("SECURITY_RIGHT");

				_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objAddNewEntity.ZADD_DATE);
_Entity.UZADD_USER     =objAddNewEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objAddNewEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objAddNewEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objAddNewEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objAddNewEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objAddNewEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objAddNewEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objAddNewEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objAddNewEntity.ZSECURITY_CODE6;

			_Entity.VO_DET_AMT=_Entity.VO_CCY_AMT*_Entity.XCHG_RATE;
			
			ZZTrace.Debug(ZFILE_NAME,_Entity.VO_DET_AMT);
			ZZTrace.Debug(ZFILE_NAME,_Entity.VO_CCY_AMT);
			if (_Entity.SIGN_CODE=="D")
			{
				_Entity.VO_CR_DET_AMT=0;
				_Entity.VO_CR_CCY_AMT=0;
				_Entity.VO_DR_DET_AMT=_Entity.VO_DET_AMT;
				_Entity.VO_DR_CCY_AMT=_Entity.VO_CCY_AMT;
			}else{
				_Entity.VO_CR_DET_AMT=_Entity.VO_DET_AMT;
				_Entity.VO_CR_CCY_AMT=_Entity.VO_CCY_AMT;
				_Entity.VO_DR_DET_AMT=0;
				_Entity.VO_DR_CCY_AMT=0;
			}

							objAddNewEntity.VO_NO=_Entity.VO_NO;
							objAddNewEntity.VO_DT=_Entity.VO_DT;
							objAddNewEntity.ACC_CODE=_Entity.ACC_CODE;
							objAddNewEntity.CCY_CODE=_Entity.CCY_CODE;
							objAddNewEntity.XCHG_RATE=_Entity.XCHG_RATE;
							objAddNewEntity.SERIAL_NO=_Entity.SERIAL_NO;
							objAddNewEntity.SIGN_CODE=_Entity.SIGN_CODE;
							objAddNewEntity.VO_DOC_NAME01=_Entity.VO_DOC_NAME01;
							objAddNewEntity.VO_RMKS=_Entity.VO_RMKS;
							objAddNewEntity.REQ_QTY=_Entity.REQ_QTY;
							objAddNewEntity.UOM_CODE=_Entity.UOM_CODE;
							objAddNewEntity.DOC_NO=_Entity.DOC_NO;
							objAddNewEntity.DOC_TP_CODE=_Entity.DOC_TP_CODE;
							objAddNewEntity.PAY_CODE=_Entity.PAY_CODE;
							objAddNewEntity.DOC_REF_NO=_Entity.DOC_REF_NO;
							objAddNewEntity.DOC_REQ_DATE=_Entity.DOC_REQ_DATE;
							objAddNewEntity.DOC_DUE_DATE=_Entity.DOC_DUE_DATE;
							objAddNewEntity.VO_DR_DET_AMT=_Entity.VO_DR_DET_AMT;
							objAddNewEntity.VO_DR_CCY_AMT=_Entity.VO_DR_CCY_AMT;
							objAddNewEntity.VO_CR_DET_AMT=_Entity.VO_CR_DET_AMT;
							objAddNewEntity.VO_CR_CCY_AMT=_Entity.VO_CR_CCY_AMT;
					ZZTrace.Debug(ZFILE_NAME,XSERIAL_NO);

					objAddNewEntity.SERIAL_NO=XSERIAL_NO;
					_Entity.SERIAL_NO=XSERIAL_NO;
			}

				this.Trans.SaveChanges(objAddNewEntity);

				// WriteDefault VORHDTM
ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_NO="+_Entity.VO_NO);
			if (!Util.IsNullOrEmpty(_Entity.VO_NO))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(VORHDTMEntity));//GLRHDTM
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("VO_NO",_Entity.VO_NO);
				_DataPermission.TABLE_NAME="VORHDTM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"VORHDTM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					VORHDTMEntity objDVORHDTMEntity=(VORHDTMEntity)objEntityContainer[0];
					if (objDVORHDTMEntity.Verified)
					{
						 _Entity.VO_NO=objDVORHDTMEntity.VO_NO;
						 _Entity.COMP_CODE=objDVORHDTMEntity.COMP_CODE;
						 _Entity.ODTP_CODE=objDVORHDTMEntity.ODTP_CODE;
						 _Entity.ODTP_STATUS=objDVORHDTMEntity.ODTP_STATUS;
						 _Entity.VO_DATE=objDVORHDTMEntity.VO_DATE;

					}else{
					}
			}else{
			}
			}

				// WriteDefault ACCDTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.ACC_CODE="+_Entity.ACC_CODE);
			if (!Util.IsNullOrEmpty(_Entity.ACC_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ACCDTBMEntity));//Chart Of Account
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ACC_CODE",_Entity.ACC_CODE);
				_DataPermission.TABLE_NAME="ACCDTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"ACCDTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					ACCDTBMEntity objDACCDTBMEntity=(ACCDTBMEntity)objEntityContainer[0];
					if (objDACCDTBMEntity.Verified)
					{
						 _Entity.ACC_CODE=objDACCDTBMEntity.ACC_CODE;
						 _Entity.ACC_NAME01=objDACCDTBMEntity.ACC_NAME01;

					}else{
					}
			}else{
			}
			}

				decimal cTOTAL_VO_DR_AMT=0;
				decimal cTOTAL_AP_CR_AMT=0;
				
				QueryRows RsVORDTLM = new QueryRows(this.Trans);
				RsVORDTLM.CommandText = "SELECT * FROM VORDTLM WHERE VO_NO='"+_Entity.VO_NO+"'";
				RsVORDTLM.Open();
				while (!RsVORDTLM.EOF)
				{
					if (RsVORDTLM.getValue("SIGN_CODE")=="D")
					{
						cTOTAL_VO_DR_AMT=cTOTAL_VO_DR_AMT+Math.Abs(RsVORDTLM.getDecimal("VO_DR_DET_AMT"));
					}else{
						cTOTAL_AP_CR_AMT=cTOTAL_AP_CR_AMT+Math.Abs(RsVORDTLM.getDecimal("VO_CR_DET_AMT"));
					}
					RsVORDTLM.MoveNext();
				}
      	RsVORDTLM.Close();
  
				_Entity.DR_DET_AMT=cTOTAL_VO_DR_AMT;
				_Entity.CR_DET_AMT=cTOTAL_AP_CR_AMT;
				_Entity.DIFF_DET_AMT=_Entity.DR_DET_AMT-_Entity.CR_DET_AMT;

				_Entity.DATA_STATUS_CODE="Verified";
			return _Entity;
		}

		public  UID02204BE Update(UID02204BE _Entity)
		{

			if (_DocsPermission.AccessRight("UID02204B", this.ZZLOGON_ID, "UPDATE")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02204B","NotUpdateRight",this.ZZLOGON_ID));
				return _Entity;
			}
			_Entity.ATVT_CODE="UPDATE";

						if (_Entity.VO_DT=="")
						{
							_Entity.Exceptions.Add(new LogisticException("VO_DT","DataMiss",""));
						}

			_Entity=this.Verify(_Entity);
			ZZTrace.Debug(ZFILE_NAME,"_Entity.Exceptions.Count="+_Entity.Exceptions.Count);
			if (_Entity.Exceptions.Count>0)
			{
				return _Entity;
			}

					//ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_NO="+_Entity.VO_NO);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_DT="+_Entity.VO_DT);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ACC_CODE="+_Entity.ACC_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.CCY_CODE="+_Entity.CCY_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.XCHG_RATE="+_Entity.XCHG_RATE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.SERIAL_NO="+_Entity.SERIAL_NO);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.SIGN_CODE="+_Entity.SIGN_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_DOC_NAME01="+_Entity.VO_DOC_NAME01);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_RMKS="+_Entity.VO_RMKS);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.REQ_QTY="+_Entity.REQ_QTY);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.UOM_CODE="+_Entity.UOM_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.DOC_NO="+_Entity.DOC_NO);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.DOC_TP_CODE="+_Entity.DOC_TP_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.PAY_CODE="+_Entity.PAY_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.DOC_REF_NO="+_Entity.DOC_REF_NO);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.DOC_REQ_DATE="+_Entity.DOC_REQ_DATE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.DOC_DUE_DATE="+_Entity.DOC_DUE_DATE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_DR_DET_AMT="+_Entity.VO_DR_DET_AMT);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_DR_CCY_AMT="+_Entity.VO_DR_CCY_AMT);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_CR_DET_AMT="+_Entity.VO_CR_DET_AMT);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_CR_CCY_AMT="+_Entity.VO_CR_CCY_AMT);

			objRetrieveEntity=new CriteriaRetrieve(typeof(VORDTLMEntity));
			objCondition=objRetrieveEntity.NewCondition();

					objCondition.Equals("VO_DT", _Entity.VO_DT);

			_DataPermission.UpdateCondition(objCondition);

			EntityObject objEntityObject=this.Trans.RetrieveEntity(objRetrieveEntity);

			if (objEntityObject != null)
			{
				VORDTLMEntity objUpdateEntity=(VORDTLMEntity)objEntityObject;
				if (objUpdateEntity.Verified)
				{
					objUpdateEntity.ZCHG_DATE=DateTime.Now;
					objUpdateEntity.ZCHG_USER=this.ZZLOGON_ID;

			_Entity.VO_DET_AMT=_Entity.VO_CCY_AMT*_Entity.XCHG_RATE;
			
			ZZTrace.Debug(ZFILE_NAME,_Entity.VO_DET_AMT);
			ZZTrace.Debug(ZFILE_NAME,_Entity.VO_CCY_AMT);
			if (_Entity.SIGN_CODE=="D")
			{
				_Entity.VO_CR_DET_AMT=0;
				_Entity.VO_CR_CCY_AMT=0;
				_Entity.VO_DR_DET_AMT=_Entity.VO_DET_AMT;
				_Entity.VO_DR_CCY_AMT=_Entity.VO_CCY_AMT;
			}else{
				_Entity.VO_CR_DET_AMT=_Entity.VO_DET_AMT;
				_Entity.VO_CR_CCY_AMT=_Entity.VO_CCY_AMT;
				_Entity.VO_DR_DET_AMT=0;
				_Entity.VO_DR_CCY_AMT=0;
			}

								objUpdateEntity.VO_NO=_Entity.VO_NO;
								objUpdateEntity.VO_DT=_Entity.VO_DT;
								objUpdateEntity.ACC_CODE=_Entity.ACC_CODE;
								objUpdateEntity.CCY_CODE=_Entity.CCY_CODE;
								objUpdateEntity.XCHG_RATE=_Entity.XCHG_RATE;
								objUpdateEntity.SERIAL_NO=_Entity.SERIAL_NO;
								objUpdateEntity.SIGN_CODE=_Entity.SIGN_CODE;
								objUpdateEntity.VO_DOC_NAME01=_Entity.VO_DOC_NAME01;
								objUpdateEntity.VO_RMKS=_Entity.VO_RMKS;
								objUpdateEntity.REQ_QTY=_Entity.REQ_QTY;
								objUpdateEntity.UOM_CODE=_Entity.UOM_CODE;
								objUpdateEntity.DOC_NO=_Entity.DOC_NO;
								objUpdateEntity.DOC_TP_CODE=_Entity.DOC_TP_CODE;
								objUpdateEntity.PAY_CODE=_Entity.PAY_CODE;
								objUpdateEntity.DOC_REF_NO=_Entity.DOC_REF_NO;
								objUpdateEntity.DOC_REQ_DATE=_Entity.DOC_REQ_DATE;
								objUpdateEntity.DOC_DUE_DATE=_Entity.DOC_DUE_DATE;
								objUpdateEntity.VO_DR_DET_AMT=_Entity.VO_DR_DET_AMT;
								objUpdateEntity.VO_DR_CCY_AMT=_Entity.VO_DR_CCY_AMT;
								objUpdateEntity.VO_CR_DET_AMT=_Entity.VO_CR_DET_AMT;
								objUpdateEntity.VO_CR_CCY_AMT=_Entity.VO_CR_CCY_AMT;

						this.Trans.SaveChanges(objUpdateEntity);

						// WriteDefault VORHDTM
ZZTrace.Debug(ZFILE_NAME,"_Entity.VO_NO="+_Entity.VO_NO);
			if (!Util.IsNullOrEmpty(_Entity.VO_NO))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(VORHDTMEntity));//GLRHDTM
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("VO_NO",_Entity.VO_NO);
				_DataPermission.TABLE_NAME="VORHDTM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"VORHDTM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					VORHDTMEntity objDVORHDTMEntity=(VORHDTMEntity)objEntityContainer[0];
					if (objDVORHDTMEntity.Verified)
					{
						 _Entity.VO_NO=objDVORHDTMEntity.VO_NO;
						 _Entity.COMP_CODE=objDVORHDTMEntity.COMP_CODE;
						 _Entity.ODTP_CODE=objDVORHDTMEntity.ODTP_CODE;
						 _Entity.ODTP_STATUS=objDVORHDTMEntity.ODTP_STATUS;
						 _Entity.VO_DATE=objDVORHDTMEntity.VO_DATE;

					}else{
					}
			}else{
			}
			}

						// WriteDefault ACCDTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.ACC_CODE="+_Entity.ACC_CODE);
			if (!Util.IsNullOrEmpty(_Entity.ACC_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ACCDTBMEntity));//Chart Of Account
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ACC_CODE",_Entity.ACC_CODE);
				_DataPermission.TABLE_NAME="ACCDTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"ACCDTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					ACCDTBMEntity objDACCDTBMEntity=(ACCDTBMEntity)objEntityContainer[0];
					if (objDACCDTBMEntity.Verified)
					{
						 _Entity.ACC_CODE=objDACCDTBMEntity.ACC_CODE;
						 _Entity.ACC_NAME01=objDACCDTBMEntity.ACC_NAME01;

					}else{
					}
			}else{
			}
			}

				decimal cTOTAL_VO_DR_AMT=0;
				decimal cTOTAL_AP_CR_AMT=0;
				
				QueryRows RsVORDTLM = new QueryRows(this.Trans);
				RsVORDTLM.CommandText = "SELECT * FROM VORDTLM WHERE VO_NO='"+_Entity.VO_NO+"'";
				RsVORDTLM.Open();
				while (!RsVORDTLM.EOF)
				{
					if (RsVORDTLM.getValue("SIGN_CODE")=="D")
					{
						cTOTAL_VO_DR_AMT=cTOTAL_VO_DR_AMT+Math.Abs(RsVORDTLM.getDecimal("VO_DR_DET_AMT"));
					}else{
						cTOTAL_AP_CR_AMT=cTOTAL_AP_CR_AMT+Math.Abs(RsVORDTLM.getDecimal("VO_CR_DET_AMT"));
					}
					RsVORDTLM.MoveNext();
				}
      	RsVORDTLM.Close();
  
				_Entity.DR_DET_AMT=cTOTAL_VO_DR_AMT;
				_Entity.CR_DET_AMT=cTOTAL_AP_CR_AMT;
				_Entity.DIFF_DET_AMT=_Entity.DR_DET_AMT-_Entity.CR_DET_AMT;

						_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objUpdateEntity.ZADD_DATE);
_Entity.UZADD_USER     =objUpdateEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objUpdateEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objUpdateEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objUpdateEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objUpdateEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objUpdateEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objUpdateEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objUpdateEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objUpdateEntity.ZSECURITY_CODE6;

					_Entity.DATA_STATUS_CODE="Verified";

				}else{
					_Entity.Exceptions.Add(new LogisticException("VO_DT","DataNotFd"));
				}
			}else{
				_Entity.Exceptions.Add(new LogisticException("VO_DT","DataNotFd"));
			}
			return _Entity;
		}

		public  UID02204BE Delete(UID02204BE _Entity)
		{
			if (_DocsPermission.AccessRight("UID02204B", this.ZZLOGON_ID, "DELETE")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02204B","NotDeleteRight"));
				return _Entity;
			}
			_Entity.ATVT_CODE="DELETE";

					if (_Entity.VO_DT=="")
					{
						_Entity.Exceptions.Add(new LogisticException("VO_DT","DataMiss"));
						return _Entity;
					}

						CriteriaRetrieve objValidateEntity=new CriteriaRetrieve(typeof(VORHDTMEntity));
						objCondition=objValidateEntity.NewCondition();
						objCondition.Equals("VO_NO",_Entity.VO_NO);
			
						EntityObject objValidateObject=this.Trans.RetrieveEntity(objValidateEntity);

						if (objValidateObject!=null)
						{
							VORHDTMEntity objVORHDTMEntity=(VORHDTMEntity)objValidateObject;
							
							ZZStatusCheck oZZStatusCheck=new ZZStatusCheck();
							oZZStatusCheck.Trans=this.Trans;
							if (!oZZStatusCheck.Validate("ODTP_CODE_DT",objVORHDTMEntity.ODTP_STATUS,_Entity.ATVT_CODE))
							{
  							throw new LogisticException("ODTP_STATUS",oZZStatusCheck.Caption,objVORHDTMEntity.ODTP_STATUS);
							}
						}

				objRetrieveEntity=new CriteriaRetrieve(typeof(VORDTLMEntity));
			objCondition=objRetrieveEntity.NewCondition();

					objCondition.Equals("VO_DT", _Entity.VO_DT);

			_DataPermission.DeleteCondition(objCondition);

			EntityObject objDeleteEntity=this.Trans.RetrieveEntity(objRetrieveEntity);

			if (objDeleteEntity != null)
			{
				if (objDeleteEntity.Verified)
				{
						this.Trans.DeleteEntity(objDeleteEntity);
				
				decimal cTOTAL_VO_DR_AMT=0;
				decimal cTOTAL_AP_CR_AMT=0;
				
				QueryRows RsVORDTLM = new QueryRows(this.Trans);
				RsVORDTLM.CommandText = "SELECT * FROM VORDTLM WHERE VO_NO='"+_Entity.VO_NO+"'";
				RsVORDTLM.Open();
				while (!RsVORDTLM.EOF)
				{
					if (RsVORDTLM.getValue("SIGN_CODE")=="D")
					{
						cTOTAL_VO_DR_AMT=cTOTAL_VO_DR_AMT+Math.Abs(RsVORDTLM.getDecimal("VO_DR_DET_AMT"));
					}else{
						cTOTAL_AP_CR_AMT=cTOTAL_AP_CR_AMT+Math.Abs(RsVORDTLM.getDecimal("VO_CR_DET_AMT"));
					}
					RsVORDTLM.MoveNext();
				}
      	RsVORDTLM.Close();
  
				_Entity.DR_DET_AMT=cTOTAL_VO_DR_AMT;
				_Entity.CR_DET_AMT=cTOTAL_AP_CR_AMT;
				_Entity.DIFF_DET_AMT=_Entity.DR_DET_AMT-_Entity.CR_DET_AMT;

						StringBuilder delete_cmd=new StringBuilder(this.ZZLOGON_ID+"UID02204B-DELETED=");
							delete_cmd.Append("VO_DT="+_Entity.VO_DT);

					ZZTrace.Debug("DELETED_LOG",delete_cmd.ToString());

				}else{
					_Entity.Exceptions.Add(new LogisticException("VO_DT","DataNotFd"));
					return _Entity;
				}
			}else{
				_Entity.Exceptions.Add(new LogisticException("VO_DT","DataNotFd"));
			}
			return _Entity;

		}

		public UID02204BE UserDefine(VContexts cMethod,UID02204BE _Entity)
		{
				return _Entity;
		}

#region Field
			private CriteriaRetrieve objRetrieveEntity;
		private List<EntityObject>  objEntityContainer;
		private Condition        objCondition;
		private ZZDefault        objDefault      = new ZZDefault();
		private ZZDataPermission _DataPermission = new ZZDataPermission();
		private ZZDocsPermission _DocsPermission = new ZZDocsPermission();
		private ZZSetting        _Setting        = new ZZSetting();
		private XConvert         _Convert        = new XConvert();

#endregion

			const  string ZFILE_NAME   ="UID02204BM";
		const  string MUKI_EDITION ="MIN";

	}
}
